package com.xc.file.web.rest;

import com.xc.file.bean.UploadBean;
import com.xc.file.config.Constants;
import com.xc.file.config.StartSuccess;
import com.xc.file.dto.UploadDto;
import com.xc.file.enums.FailCode;
import com.xc.file.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 上传文件请求接口</p>
 * <P>创建人: 小程</p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/8/14</p>
 *
 * @version v1.0
 */
@RestController
public class UploadFileRest {

    @Autowired
    private UploadFileService uploadFileService;
    @Autowired
    private Constants constants;
    @Autowired
    private StartSuccess startSuccess;

    /**
     * 上传文件
     *
     * @param file       文件
     * @param uploadBean 参数
     * @return 结果
     */
    @PostMapping("/file/upload")
    public UploadDto uploadFile(MultipartFile file, UploadBean uploadBean) {
        if (file == null) {
            throw FailCode.FILE_NOT_EMPTY.getOperateException();
        }
        return uploadFileService.uploadFile(file, uploadBean);
    }

    /**
     * 删除文件
     *
     * @param hashCodes 文件hashCode集合
     */
    @DeleteMapping("/file/delete/{hashCodes}")
    public void deleteFile(@PathVariable("hashCodes") List<String> hashCodes,
                           @RequestParam(value = "secret", required = false) String secret) {
        if (constants.getOpenNotice()) {
            if (!constants.getSecret().equals(secret)) {
                throw FailCode.NOT_HAVE_AUTHORITY.getOperateException();
            }
        }
        uploadFileService.deleteFile(hashCodes);
    }

    /**
     * 检测状态
     *
     * @return 是否成功
     */
    @GetMapping("/check/state")
    public Boolean verifyState() {
        return true;
    }

    /**
     * 同步数据
     */
    @GetMapping("/sync")
    public Boolean sync() {
        startSuccess.notice();
        return true;
    }

}
